/**
 * 
 */
package de.homeR.android.pluginmanagement;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.homeR.android.R;

/**
 * @author André Stannek
 * 
 */
public class PluginManagementListAdapter extends
		ArrayAdapter<PluginManagementListItemModel> {

	/**
	 * 
	 */
	private final List<PluginManagementListItemModel> list;

	/**
	 * 
	 */
	private final Activity context;

	/**
	 * @param context
	 *            the context
	 * @param list
	 *            the list of plugins
	 */
	public PluginManagementListAdapter(final Activity context,
			final List<PluginManagementListItemModel> list) {
		super(context, R.layout.pluginmanagmentlistlayout, list);
		this.context = context;
		this.list = list;
	}

	/**
	 * @author André Stannek
	 * 
	 */
	static class ViewHolder {

		/**
		 * 
		 */
		private TextView text;

		/**
		 * @return the text
		 */
		public TextView getText() {
			return text;
		}

		/**
		 * @param text
		 *            the text to set
		 */
		public void setText(final TextView text) {
			this.text = text;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public final View getView(final int position, final View convertView,
			final ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.pluginmanagmentlistlayout, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.setText((TextView) view.findViewById(R.id.label));
			view.setTag(viewHolder);
		} else {
			view = convertView;
		}
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.getText().setText(list.get(position).getName());
		return view;
	}
}
